/**
 * JSparrow - Une version numérique du jeu "Attention Pirates !" - Copyright (C) 2014 Black Pearl
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with this program. If
 * not, see <http://www.gnu.org/licenses/>.
 */
package App.Modele;

import java.io.Serializable;

/**
 * Classe de gestion de repaire.
 *
 * @author BlackPearl
 */
public class Repaire extends EntiteCase implements Serializable {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public int getNbPiratesMax() {
		return nbPiratesMax;
	}

	public int getNbCanonsMax() {
		return nbCanonsMax;
	}

	public int getNbCanonsFixes() {
		return nbCanonsFixes;
	}

	private int nbPiratesMax;
    private int nbCanonsMax;
    private int nbCanonsFixes;
    private int id;
    private static int lastId = 0;

    /**
     * Initalisation du contexte.
     *
     * @param nbTresors nombre de trésors du repaire
     * @param nbPirates nombre de pirates du repaire
     * @param nbCanons nombre de canons du repaire
     * @param nbCanonsFixes nombre de canons fixes du repaire
     */
    public Repaire(int nbTresors, int nbPirates, int nbCanons, int nbCanonsFixes) {
        super(nbTresors, nbPirates, nbCanons);
        this.id = lastId++;
        this.nbPiratesMax = nbPirates;
        this.nbCanonsMax = nbCanons;
        this.nbCanonsFixes = nbCanonsFixes;
        int i = 0;
        while( i < nbCanonsMax)
        {
        	arrayPiece[i] = TypePiece.Canon;
        	i++;
        }
        while(i < nbCanonsMax+nbPiratesMax)
        {
        	arrayPiece[i] = TypePiece.Pirate;
        	i++;
        }
        while( i < nbCanonsMax+nbPiratesMax+this.nbCanonsFixes)
        {
        	arrayPiece[i] = TypePiece.Fixe;
        	i++;
        }
    }
    
    public int getId(){
    	return id;
    }

    @Override
    public String toString() {
        StringBuilder repaireStringBuffer = new StringBuilder();
        int num;
        
        for (int i = 0; i < arrayPiece.length; i++) {
            num = i + 1;
            if (arrayPiece[i] == TypePiece.Pirate) {
            	repaireStringBuffer.append("Piece n°" + num + ": Pirate; ");
            } else if (arrayPiece[i] == TypePiece.Canon) {
            	repaireStringBuffer.append("Piece n°" + num + ": Canon; ");
            } else if (arrayPiece[i] == TypePiece.Tresor) {
            	repaireStringBuffer.append("Piece n°" + num + ": Tresor; ");
            } else if (arrayPiece[i] == TypePiece.Fixe) {
            	repaireStringBuffer.append("Piece n°" + num + ": Fixe; ");
            } else if (arrayPiece[i] == TypePiece.Vide) {
            	repaireStringBuffer.append("Piece n°" + num + ": Vide; ");
            } else {
            	repaireStringBuffer.append("Piece n°" + num + ": Vide; ");
            }
        }
        repaireStringBuffer.append("nbTresors: " + this.getNbTresors() + "; ");
        repaireStringBuffer.append("nbPirates:" + this.getNbPirates() + "; ");
        repaireStringBuffer.append("nbCanons: " + this.getNbCanons() + "; ");
        return repaireStringBuffer.toString();
    }
    
    public String toCom(){
    	StringBuilder repaireStringBuffer = new StringBuilder();
        int num;
        for (int i = 0; i < arrayPiece.length; i++) {
            num = i + 1;
            if (arrayPiece[i] == TypePiece.Pirate) {
            	repaireStringBuffer.append("1");
            } else if (arrayPiece[i] == TypePiece.Canon) {
            	repaireStringBuffer.append("2");
            } else if (arrayPiece[i] == TypePiece.Tresor) {
            	repaireStringBuffer.append("3");
            } else if (arrayPiece[i] == TypePiece.Fixe) {
            	repaireStringBuffer.append("4");
            } else if (arrayPiece[i] == TypePiece.Vide) {
            	repaireStringBuffer.append("0");
            } else {
            	repaireStringBuffer.append("0");
            }
            if(num < arrayPiece.length){
            	repaireStringBuffer.append("-");
            }
        }
        repaireStringBuffer.append("_");
        repaireStringBuffer.append( this.getNbTresors());
        repaireStringBuffer.append("_");
        repaireStringBuffer.append( this.getId());
        return repaireStringBuffer.toString();
    }
}
